<template>
  <el-dialog
    title="价格模式"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    width="650px"
    :before-close="cancel">
    <el-form ref="elForm" :model="form" size="small" label-width="100px">
      <div class="card">
        <el-row style="margin-bottom: -30px">
          <el-form-item label-width="0" prop="priceMode">
            <el-radio-group v-model="form.priceMode" @change="resetPriceModeForm">
              <el-radio :label="1">{{ $t('固定价格') }}</el-radio>
              <el-radio :label="2" style="margin-left: 170px">{{ $t('动态价格') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12" style="padding-right: 100px">
            <p style="background-color: #eee; padding: 10px; border-radius: 5px">
              {{ $t('用于手动调整云价格') }}
            </p>
          </el-col>
          <el-col :span="12">
            <p style="background-color: #eee; padding: 10px; border-radius: 5px">
              {{
                $t(
                  '根据采购价格变动（上架时变动）可以设置倍率自动变动（默认倍率是1）例如采购价格是200，倍率是1.2，那么上架时当前在库的云价格就会自动更新成240',
                )
              }}
            </p>
          </el-col>
        </el-row>
        <!-- 固定价格 -->
        <el-row v-if="form.priceMode == 1" style="margin: 30px 0">
          <el-col :span="12">
            <el-form-item label-width="60px" :label="$t('云价格') + ':'" prop="cloudPrice">
              <el-input-number
                style="margin-right: 10px"
                v-model="form.cloudPrice"
                controls-position="right"
                :precision="0"
                :min="0" />JPY
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="50px" :label="$t('价格') + ':'" prop="cloudPrice">
              <span style="margin: 0 10px">{{ form.price }}</span
              >JPY
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 动态价格 -->
        <el-row v-if="form.priceMode == 2" style="margin: 30px 0">
          <el-col :span="8">
            <el-form-item label-width="80px" :label="$t('倍率') + ':'" prop="cloudPrice">
              <el-input-number
                style="margin-right: 10px"
                @change="rateChange"
                v-model="form.multipleRate"
                controls-position="right"
                :precision="2"
                :min="1" />
            </el-form-item>
          </el-col>
          <el-col :span="7" :offset="1">
            <el-form-item label-width="50px" :label="$t('价格') + ':'" prop="cloudPrice">
              <span style="margin: 0 10px">{{ form.price }}</span>
              JPY
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label-width="60px" :label="$t('云价格') + ':'" prop="cloudPrice">
              <span style="margin: 0 10px">{{ (form.multipleRate * form.price).toFixed(0) }}</span>
              JPY
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">
        <!-- 关闭 -->
        {{ $t('关闭') }}
      </el-button>
      <el-button type="primary" @click="handleSubmit">{{ $t('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { skuDistributorSetPriceMode } from '@/api/distribution/sku/shopSkuPair'
export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },

    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      form: {},
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.formData).forEach((key) => {
          this.$set(this.form, key, this.formData[key])
        })
        console.log(`this.form ==>`, JSON.parse(JSON.stringify(this.form)))
      }
    },
  },
  methods: {
    handleSubmit() {
      const data = JSON.parse(JSON.stringify(this.form))
      data.fixedPrice =data.cloudPrice
      skuDistributorSetPriceMode(data).then((res) => {
        if (res.code === 200) {
          this.$message({ type: 'success', message: res.msg })
          this.$emit('getList')
          this.cancel()
        }
      })
    },
    resetPriceModeForm() {
      this.form.cloudPrice = this.formData.cloudPrice
      this.form.multipleRate = this.formData.multipleRate
    },
    rateChange(rate) {
      this.form.cloudPrice = (this.form.price * rate).toFixed(4)
    },
    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
